package com.lsp.microorder.recharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lsp.microcommon.entity.RechargeInfo;
import com.lsp.microcommon.utils.UserUtils;
import com.lsp.microorder.pay.param.PayResultParam;
import com.lsp.microorder.recharge.dao.RechargeInfoDao;
import com.lsp.microorder.recharge.service.RechargeInfoService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class RechargeInfoServiceImpl implements RechargeInfoService {

    @Resource
    private RechargeInfoDao rechargeInfoDao;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Integer addRecharge(RechargeInfo rechargeInfo) {
        Integer uid = UserUtils.getUser();
//        设置了充值的数据库，前端充值时获取订单id
        String orderNo = UUID.randomUUID().toString().replace("-", "");
        rechargeInfo.setUid(uid);
        rechargeInfo.setOrderNo(orderNo);
//        使用mybatisplus时，添加时会自动获取自增值
        rechargeInfoDao.insert(rechargeInfo);
//        向mq发送消息

//        这个内部使用死信
//        sendMsg(rechargeInfo.getId().toString());
        sendDelayMsg(rechargeInfo.getId().toString());
        return rechargeInfo.getId();
    }

    @Override
    public RechargeInfo rechargeInfo(Integer id) {
        return rechargeInfoDao.selectById(id);
    }

    @Override
    public RechargeInfo selectInfoByOrderId(String orderId) {
        return rechargeInfoDao.selectOne(new LambdaQueryWrapper<RechargeInfo>()
                .eq(RechargeInfo::getOrderNo,orderId));
    }

    @Override
    public void updateRechargeInfo(PayResultParam param) {
        rechargeInfoDao.update(null,
                new LambdaUpdateWrapper<RechargeInfo>()
                        .set(RechargeInfo::getPayTradeNo,param.getPayTradeNo())
                        .set(RechargeInfo::getStateFlag,2)
                        .eq(RechargeInfo::getOrderNo,param.getOrderNo()));
    }

    @Override
    public void updatePayFlag(String orderNo) {
        rechargeInfoDao.update(null,
                new LambdaUpdateWrapper<RechargeInfo>()
                        .set(RechargeInfo::getStateFlag,2)
                        .eq(RechargeInfo::getOrderNo,orderNo));
    }

    @Override
    public void updateRechargeStatus(Integer orderId, Integer state) {
        rechargeInfoDao.update(null, new LambdaUpdateWrapper<RechargeInfo>()
                .set(RechargeInfo::getStateFlag,state)
                .eq(RechargeInfo::getId,orderId));
    }


    /**
     * 向订单队列中发送消息
     */
    public void sendMsg(String orderId) {
//        向普通的订单交换机发送消息
        rabbitTemplate.convertAndSend("order_ex", "order_key", orderId, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置过期时间30s
                message.getMessageProperties().setExpiration("30000");
                return message;
            }
        });
    }


    public void sendDelayMsg(String orderId) {
//        向普通的订单交换机发送消息
        rabbitTemplate.convertAndSend("order_delay_ex", "order_delay_key", orderId, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置过期时间30s
                message.getMessageProperties().setDelay(30000);
                return message;
            }
        });
    }
}
